/**
 * craete  by Jane
 * APP应用相关接口
 */
import { getAction, postAction, putAction } from './manage'

let func = (data) => menu.setForEachMenu(data)

class menu {
    // 整理应用菜单格式
    static setForEachMenu(data = []) {
        if (data.length) {
            data.forEach((item) => {
                item['value'] = item['id']
                if (item.children && item.children.length) {
                    item.children = func(item.children)
                } else {
                    item.children = null
                }
            })
        }
        return data
    }
}

// 查询APP应用所有菜单
export const getAppAllMenuList = async (params) => {
    let res = await getAction('/hc/hcBackRoleMenu/getHcBackRoleMenuList', params)
    if (res && res.success) {
        return menu.setForEachMenu(res.result)
    }
    return []
}

// 根据角色查询应用菜单权限
export const getAppMenuListByRole = async (params) => {
    if (params.roleIds) {
        let res = await getAction('/hc/hcBackRoleMenu/getHcBackRoleMenuListByRoleIds', params)
        if (res && res.success) {
            let ids = []
            const setMenuData = (data = []) => {
                if (data.length) {
                    data.forEach((item) => {
                        ids.push(item['id'])
                        if (item && item.children && item.children.length) {
                            setMenuData(item.children)
                        }
                    })
                }
            }
            setMenuData(res.result || [])
            return {
                data: res.result || [],
                ids
            }
        }
    }
    return []
}

// 当前角色保存  菜单权限
export const saveAppMenuByRole = async (params) => {
    return await putAction('/hc/hcBackRoleMenu/batchEdit', params)
}

// 所有文章类型
export const getArticleTypeList = async (params) => {
    let res = await getAction('/hc/articleType/queryList', params);
    let list = [];
    if(res && res.success){
        let data = res.result || [];
        data.forEach((item,index) => {
            list.push({
                label : item['name'],
                value : item['id'],
            })
        })
    }
    return list;
}

